package com.starblink.discover.cell

import android.view.View
import androidx.fragment.app.FragmentActivity
import com.starblink.android.basic.adapter.BaseBindingVH
import com.starblink.android.basic.ext.click
import com.starblink.android.basic.ext.visibleOrGone
import com.starblink.android.basic.extension.navigationTo
import com.starblink.android.basic.sensorsdata.extension.businessSuffixId
import com.starblink.android.basic.sensorsdata.extension.trackData
import com.starblink.android.basic.sensorsdata.spm.cfg.SpmElementDef
import com.starblink.android.basic.sensorsdata.spm.cfg.SpmPageDef
import com.starblink.android.basic.util.ImageUtils
import com.starblink.basic.route.RoutePage
import com.starblink.home.databinding.ItemPostStarCardBinding
import com.starblink.rocketreserver.FetchRecommendPostV2Query

/**
 * 发现：明星卡片列表项
 * @note 点击图片，跳转到图搜页面
 */
class PostStarCardCell(
    val activity: FragmentActivity?,
    binding: ItemPostStarCardBinding,
) :
    BaseBindingVH<FetchRecommendPostV2Query.StarCard, ItemPostStarCardBinding>(binding = binding) {

    override fun onBind(item: FetchRecommendPostV2Query.StarCard, position: Int) {
        binding.tvTitle.text = item.title
        ImageUtils.loadImage(
            activity,
            item.mainPic,
            binding.ivMainPic,
        )
        itemView.trackData(
            page = SpmPageDef.RecommendPage,
            element = SpmElementDef.DiscoverStarCard,
            params = mapOf(
                "index" to "$position",
                "id" to "${item.id}",
                "traceId" to "${item.traceId}"
            ), businessSuffixID = businessSuffixId(item)
        )
        if (item.mainPic.isNullOrEmpty().not()) {
            binding.ivMainPic.click {
                goToImgSearchPage(item.mainPic)
            }
            binding.ivMainPic.trackDataT(position, item, 0)
        } else {
            binding.ivMainPic.setOnClickListener(null)
        }
        if (item.otherPics == null || item.otherPics!!.size < 3) {
            binding.ivProductPic2.visibleOrGone(false)
            ImageUtils.loadImage(
                activity,
                if (item.otherPics == null || item.otherPics!!.isEmpty()) "" else item.otherPics!![0],
                binding.ivProductPic1,
            )
            ImageUtils.loadImage(
                activity,
                if (item.otherPics == null || item.otherPics!!.size < 2) "" else item.otherPics!![1],
                binding.ivProductPic3,
            )
            if (item.otherPics == null || item.otherPics!!.isEmpty()) {
                binding.ivProductPic1.setOnClickListener(null)
                binding.ivProductPic3.setOnClickListener(null)
            } else if (item.otherPics!!.size < 2) {
                binding.ivProductPic3.setOnClickListener(null)
            }
            if (item.otherPics != null && item.otherPics!!.isNotEmpty()) {
                binding.ivProductPic1.click {
                    goToImgSearchPage(item.otherPics!![0])
                }
                if (item.otherPics!!.size == 2) {
                    binding.ivProductPic3.click {
                        goToImgSearchPage(item.otherPics!![1])
                    }
                }
            }
        } else {
            binding.ivProductPic2.visibleOrGone(true)
            ImageUtils.loadImage(
                activity,
                item.otherPics!![0],
                binding.ivProductPic1,
            )
            ImageUtils.loadImage(
                activity,
                item.otherPics!![1],
                binding.ivProductPic2,
            )
            ImageUtils.loadImage(
                activity,
                item.otherPics!![2],
                binding.ivProductPic3,
            )
            binding.ivProductPic1.click {
                goToImgSearchPage(item.otherPics!![0])
            }
            binding.ivProductPic2.click {
                goToImgSearchPage(item.otherPics!![1])
            }
            binding.ivProductPic3.click {
                goToImgSearchPage(item.otherPics!![2])
            }
            binding.ivProductPic1.trackDataT(position, item, 1)
            binding.ivProductPic2.trackDataT(position, item, 2)
            binding.ivProductPic3.trackDataT(position, item, 3)
        }
    }

    private fun View.trackDataT(
        position: Int,
        item: FetchRecommendPostV2Query.StarCard,
        index: Int
    ) {
        trackData(
            page = SpmPageDef.RecommendPage,
            element = SpmElementDef.DiscoverStarCard,
            onlyHit = true,
            params = mapOf(
                "index" to "$position",
                "imageIndex" to "$index",
                "id" to "${item.id}",
                "traceId" to "${item.traceId}"
            )
        )
    }

    private fun goToImgSearchPage(url: String?) {
        navigationTo(RoutePage.ImageSearch.PAGE_RESULT) {
            withString(RoutePage.ImageSearch.ARGS_IMG_URL, url)
        }
    }
}